//
//  DebugWindow.swift
//  YKSwiftDebugManager
//
//  Created by edward on 2021/12/1.
//

import UIKit

internal class DebugWindow: UIWindow {

    private var clickDebugWindow:(()->Void)? = nil
    
    private lazy var button:UIButton = {
        let btn = UIButton(type: .custom)
        btn.clipsToBounds = true
        btn.frame = self.bounds
        btn.setImage(UIImage(named: "ic_out_window"), for: .normal)
        btn.layer.cornerRadius = self.bounds.size.height/2
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    public convenience init(frame: CGRect, clickCallBack: (()->Void)?) {
        self.init(frame: frame)
        self.clickDebugWindow = clickCallBack
    }
    
    public
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        if #available(iOS 13.0, *) {
            let scene = (UIApplication.shared.connectedScenes as NSSet).anyObject() as? UIScene
            if scene != nil {
                windowScene = (scene! as! UIWindowScene)
            }
        }
        backgroundColor = .clear
        tag = 9998
        self.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
        self.layer.shadowOffset = CGSize(width: 2.5, height: 2.5)
        self.layer.shadowRadius = 5;
        self.layer.shadowOpacity = 0.5
        
        self.windowLevel = .statusBar + 50.0
        if self.rootViewController == nil {
            self.rootViewController = UIViewController.init()
        }
        button.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        self.rootViewController!.view.addSubview(button)
        
        let pan = UIPanGestureRecognizer.init(target: self, action: #selector(locationChange(pan:)))
        pan.delaysTouchesBegan = true
        self.addGestureRecognizer(pan)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func locationChange(pan:UIPanGestureRecognizer)->Void{
        let panView = self
        let tranp = pan.translation(in: panView)
        var safeBottom:CGFloat = 0
        if #available(iOS 11.0, *) {
            safeBottom = self.safeAreaInsets.bottom
        } else {
            safeBottom = 0
        }
        pan.setTranslation(CGPoint(x: 0, y: 0), in: panView)
        var newX = panView.center.x + tranp.x
        var newY = panView.center.y + tranp.y
        if (newX < UIScreen.main.bounds.size.width/2) {
            newX = self.bounds.size.width/2
        }else{
            newX = UIScreen.main.bounds.size.width - (self.bounds.size.width/2)
        }
        
        if (newY > ((UIScreen.main.bounds.maxY)-safeBottom-(self.bounds.size.width/2))) {
            newY = ((UIScreen.main.bounds.maxY)-safeBottom-(self.bounds.size.width/2))
        }
        if (newY < ((UIApplication.shared.statusBarFrame.size.height)+(self.bounds.size.width/2))) {
            newY = ((UIApplication.shared.statusBarFrame.size.height)+(self.bounds.size.width/2))
        }
        
        if pan.state == .changed {
            self.center = CGPoint(x: (panView.center.x + tranp.x), y: (panView.center.y + tranp.y))
        }else if (pan.state == .ended){
            UIView.animate(withDuration: 0.2) {
                self.center = CGPoint(x: newX, y: newY)
            }
        }
    }
    
    @objc private func clickButton(sender:UIButton)->Void{
        if clickDebugWindow != nil {
            clickDebugWindow!()
        }
    }
}
